using System;
using ValidationFramework.Configuration.Reflection;

namespace ValidationFramework.Specifications
{
    /// <summary>
    /// Specifies that a <see cref="RequiredRule{T}"/>, that will check the existance of a <see cref="byte"/>, should be applied to the program element.
    /// </summary>
    /// <seealso cref="RequiredRule{T}"/>
    /// <example>
    /// <code source="Examples\ExampleLibraryCSharp\Specifications\RequiredSpecifications\RequiredByteRuleAttributeExample.cs" lang="cs"/>
    /// <code source="Examples\ExampleLibraryVB\Specifications\RequiredSpecifications\RequiredByteRuleAttributeExample.vb" lang="vbnet"/>
	/// </example>
#if (!SILVERLIGHT)
    [Serializable]
#endif
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
	public sealed class IsRequiredByteAttribute : RuleAttribute
    {
        // --- Fields

        private byte? initialValue;


        // --- Properties

        /// <summary>
        /// Gets or sets the initial and invalid value.
        /// </summary>
        /// <seealso cref="RequiredRule{T}.InitialValue"/>
        public byte InitialValue
        {
            get
            {
                return initialValue.Value;
            }
            set
            {
                initialValue = value;
            }
        }


        // --- Methods

		/// <inheritdoc/>
		public override ISpecification CreateSpecification(ICachedReflectionInfo descriptor)
		{
			if (initialValue.HasValue)
			{
				return new IsRequiredSpec<byte>(initialValue.Value);
			}
			else
			{
				return new IsRequiredSpec<byte>();
			}
		}

    }
}